package com.cn.jdia.db.utils;

import java.util.List;
import java.util.Map;

import com.cn.jdia.db.manager.DBManager4Mysql;

public class UpdateUtils {

	public static void update(List<Map<String, String>> list) {

		DBManager4Mysql db = DBManager4Mysql.createInstance();
		db.connectDB(ConfigUtils.URL, ConfigUtils.USER, ConfigUtils.PASS);

		String update = null;
		for (Map<String, String> map : list) {
			System.out.println(map.get("servername") + map.get("ip") + "" + map.get("checked"));

			update = "update dbconfig set checked='" + map.get("checked") + "' where servername='"
					+ map.get("servername") + "' and ip='" + map.get("ip") + "'";
			db.executeUpdate(update);
		}

		System.out.println("-------");

		// update connect number
		String del = "delete from dbconnect ";
		int delret = db.executeUpdate(del);
		System.out.println("delete connect table rows: " + delret);

		String insert = null;

		for (Map<String, String> map : list) {
			if ("right".equals(map.get("checked"))) {
				// System.out.println(map.get("servername") + " " +
				// map.get("max") + " " + map.get("active"));
				insert = "insert into dbconnect (servername,ip,maxconnect,activeconnect,warn) values ('"
						+ map.get("servername") + "','" + map.get("ip") + "'," + map.get("max") + ","
						+ map.get("active")+","+map.get("warn") + ")";

				System.out.println(insert);
				db.executeUpdate(insert);
			}
		}

		db.closeDB();
	}
}
